package com.aaa.spring.service;

import com.aaa.spring.exception.BizException;
import com.aaa.spring.mapper.UserMapper;
import com.aaa.spring.mapper.WalletMapper;
import com.aaa.spring.pojo.Result;
import com.aaa.spring.pojo.User;
import com.aaa.spring.pojo.Wallet;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户服务
 */
@Service
public class UserService {
    //声明日志变量
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired //自动注入
    private UserMapper userMapper;
    @Autowired //自动注入
    private WalletMapper walletMapper;

    //登录业务判断
    public boolean login(User user) {
        if(user.getName() == null || user.getName().equals("")){

        }



        //我们需要根据用户名和密码查询数据库有没有这个用户
        User u = userMapper.getUserByNameAndPwd(user);
        //如果有这个用户 那么就返回true 让他登录
        if (u != null) {
            return true;
        }
        //如果没有这个用户 那么就返回false 不让登录
        return false;
    }

    /**
     * 注册
     *
     * @param user
     */
    public Result register(User user) {
        Result result = new Result();
        //name  password
        if(StringUtils.isEmpty(user.getName())){
            throw new BizException(500,"用户名不能为空");
        }
        if(StringUtils.isEmpty(user.getPassword())){
            throw new BizException(500,"密码不能为空");
        }
        //如果数据库有这个人 根据名称查询
        User u = userMapper.getUserByName(user);
        //那么就不让注册
        if (u != null) {
            throw new BizException(500,"该用户名已被占用!");
        }
        //如果没有
        //那么把用户信息存到数据库
        userMapper.insertUser(user);

        result.setCode(200);//正常
        result.setMsg("注册成功");
        return result;
    }

    public Result getAllUsers(Integer pageNum, Integer pageSize) {
        Result result = new Result();
        //最近执行的sql会被分页
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.getAllUsers();//最近执行的sql
        //包装成具有total(总数据) hasPreviousPage 是否有前一页 hasNextPage 是否有下一页 等属性的对象
        //list存在于pageInfo.list属性中
        PageInfo<User> pageInfo = new PageInfo<>(list);

        result.setCode(200);//正常
        result.setMsg("数据获取成功");
        Map<String,Object> map = new HashMap<>();
        map.put("pageInfo",pageInfo);
        result.setMap(map);
        return result;
    }

    /**
     * 转账
     *  @param fromId 从fromId用户转
     * @param toId   到toId用户钱包
     * @param money  转多少钱
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result transfer(Integer fromId, Integer toId, BigDecimal money) {

        //从fromId这个用户扣钱
        walletMapper.reduceMoney(fromId, money);

        //判断fromId用户余额是否足够 不够则抛出异常
        Wallet wallet = walletMapper.getWalletByUserId(fromId);
        if(wallet == null){
            throw new BizException(500,"该用户是非法用户!");
        }
//        -1 左边大
//        0 相等
//        1 右边大
        if (wallet.getMoney().compareTo(BigDecimal.ZERO) < 0) {
            //余额相比转账金额 小   先查再判断
            //余额相比0 小         先修改再判断
//            throw new RuntimeException();
            throw new BizException(500,"该用户余额不足!");
        }

        //给toId用户加钱
        walletMapper.addMoney(toId, money);

        Result result = new Result();
        result.setCode(200);//约定一个正确的code
        result.setMsg("交易成功");
        Map<String,Object> map = new HashMap<>();
//        map.put("","");
        result.setMap(map);
        return result;
    }
}
